package com.imooc.distributedemo.resume.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * leetcode49  字母异位词分组
 */
public class Solution49 {

    public static void main(String[] args) {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> results = groupAnagrams(strs);
        System.out.println(results);
    }

    public static List<List<String>> groupAnagrams(String[] strs) {
        Map<String,List<String>> map = new HashMap<>();
        for (String str:strs){
            char[] array = str.toCharArray();
            Arrays.sort(array);
            String key = new String(array);
            List<String> list = map.getOrDefault(key,new ArrayList<String>());
            list.add(str);
            map.put(key,list);
        }
        return new ArrayList<>(map.values());
        /*Collection<List<String>> values = Arrays.stream(strs).collect(Collectors.groupingBy(s -> Arrays.toString(s.codePoints().sorted().toArray()))).values();
        String s1 = "eat";
        String s2 = "ate";
        String s3 = "tee";
        IntStream intStream1 = s1.codePoints().sorted();
        IntStream intStream2 = s2.codePoints();
        IntStream intStream3 = s3.codePoints();
        return new ArrayList<>();*/
    }
}
